package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CORSConf {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurer() {
                //配置虚拟路径和实体路径的映射关系
                @Override
                public void addResourceHandlers(ResourceHandlerRegistry registry) {
                    registry.addResourceHandler("/images/***")
                            .addResourceLocations("file:D:/file/");
                }

                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**")      // 设置映射
                            .allowedOrigins("*")        // 设置域
                            .allowedMethods("*")               // 设置请求的方式GET、POST等
                            .allowedHeaders("*");              // 设置头
                }

                @Override
                public void addInterceptors(InterceptorRegistry registry) {
                    registry.addInterceptor(new MyInterceptor())
                            .addPathPatterns("/**/**")
                            .excludePathPatterns("/noLogin/**");
                }
            };
        }
}
